package com.glory.wxmemsicpda.base

import android.app.Activity
import android.content.Context
import android.view.KeyEvent
import android.widget.EditText
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.wxmemsicpda.utils.EditTextEnterUtils
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils

/**
 * @author HX
 * @date 2019/12/5
 * @desc
 */
open class BaseDialogActivity : FreeUI_BaseActivity<Any?>() {
    override fun getLayoutResId(): Int {
        return 0
    }
    interface editTextListener {
        fun onTextChangeListener(s: String)
    }
    interface onSubmitListener {
        fun setOnSubmitListener()
    }
    override fun initView() {}
    override fun initData() {}
    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return null
    }

    /**
     * 不获取焦点初始化editText
     * @param editText
     * @param listener
     * @param
     */
    protected open fun initEdit(
        editText: EditText?,
        listener: editTextListener
    ) {
        EditTextEnterUtils.initEnterToUpperCase(editText)
        FreeApi_EditTextUtils.setEditTextListener(editText, this) { s: String ->
            var replace = s.replace(" ", "")
            listener?.onTextChangeListener(replace)
        }
    }

    protected open fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    open fun showSubmitDialog(listener: onSubmitListener?) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要提交?", {
            //确定
            listener?.setOnSubmitListener()
        }) {}.show(supportFragmentManager, "SUBMIT_DIALOG")
    }

    protected open fun showConfirmDialog(message: String?) {
        FreeApi_DialogUtils.getConfirmDialog(this, "确定", message) {}.show(
            supportFragmentManager, "CONFIRM_DIALOG"
        )
    }
    protected open fun showConfirmDialog(context: Activity, message: String?) {
        FreeApi_DialogUtils.getConfirmDialog(this, "确定", message) { context.finish() }.show(
            supportFragmentManager, "CONFIRM_DIALOG"
        )
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        // 当按下返回键时所执行的命令
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            showBackDialog(this)
            false
        } else {
            super.onKeyDown(keyCode, event)
        }
    }


    protected fun showBackDialog(context: Context) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(
            this, "确定要退出当前界面吗？", { (context as Activity).finish() }) {

        }.show(supportFragmentManager, "DIALOG")
    }
}